/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.unitn.science.matr155863_148346_145963.bccstore.items;

import it.unitn.science.matr155863_148346_145963.bccstore.DBManager;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.sound.midi.SysexMessage;

/**
 *
 * @author berto
 */
public class ProductDetailsValidator {
    
          
    
    private static Date dateFromString(String string) {
        try {
            SimpleDateFormat sf = new SimpleDateFormat("dd/MM/yyyy",Locale.ITALIAN);
            sf.setLenient(false);
            Date date=sf.parse(string);
            Calendar today = Calendar.getInstance();
            today.clear(Calendar.HOUR); today.clear(Calendar.MINUTE); today.clear(Calendar.SECOND);
            Date todayDate = today.getTime();
            if(date.compareTo(todayDate)>0){
                return null;
            }
            return date;
        } catch (Exception e) {
            return null;
        }
    }
    
    public  static Product createProduct(HttpServletRequest request,DBManager db) throws Exception{
        Product product=new Product();
        String name=request.getParameter(Product.NAME);
        if(!isValidString(name)){
            throw new RuntimeException(""+Product.NAME);
        }else{
            product.setName(name);
        }
        String errOn=Product.PRICE;
        try{
            Float price=Float.parseFloat(request.getParameter(Product.PRICE));
            product.setPrice(price);
            errOn=Product.QUANTITY;
            Integer quantity=Integer.parseInt(request.getParameter(Product.QUANTITY));
            product.setQuantity(quantity);
        }catch(Exception ex){
            throw new Exception(""+errOn);
        }
        String imageStr=request.getParameter(Product.IMAGE);
        try{
            int imageId=Integer.parseInt(imageStr);
            Image image=db.getImage(imageId);
            if(image!=null){
                product.setImageId(image.getId());
                product.setImageName(image.getName());
            }else{
                throw new RuntimeException(""+Product.IMAGE);
            }
        }catch(Exception ex){
            throw new RuntimeException(""+Product.IMAGE);
        }
        String description=request.getParameter(Product.DESCRIPTION);
        if(!isValidString(description)){
            throw new RuntimeException(""+Product.DESCRIPTION);
        }else{
            product.setDescription(description);
        }
        try{
            Integer category=Integer.parseInt(request.getParameter(Product.CATEGORY));
            String categoryName=db.categoryExist(category);
            if(categoryName!=null){
                product.setCategoryId(category);
                product.setCategoryName(categoryName);
            }else{
                throw new RuntimeException(""+Product.CATEGORY);
            }
        }catch(Exception ex){
            throw new Exception(""+Product.CATEGORY);
        }
        try{
            Integer um=Integer.parseInt(request.getParameter(Product.UM));
            String umName=db.umExist(um);
            if(umName!=null){
                product.setUmId(um);
                product.setUmName(umName);
            }else{
                throw new RuntimeException(""+Product.UM);
            }
        }catch(Exception ex){
            throw new Exception(""+Product.UM);
        }
        return product;
    }
    
    private static boolean isValidString(String string){
        boolean valid=false;
        if(string!=null){
            int i=0;
            while((valid==false)&&(i<string.length())){
                if(string.charAt(i)==' '){
                    i++;
                }else{
                    valid=true;
                }
            }
        }
        return valid;
    }
    
}
